package com.wxzz.elearing.livetelecast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.livetelecast.dto.LivetelecastCategoryDto;
import com.wxzz.elearing.livetelecast.entity.LivetelecastCategory;
import com.wxzz.elearing.livetelecast.entity.PlatformLivetelecast;
import com.wxzz.elearing.livetelecast.mapper.LivetelecastCategoryMapper;
import com.wxzz.elearing.livetelecast.mapper.PlatformLivetelecastMapper;
import com.wxzz.elearing.livetelecast.service.ILivetelecastCategoryService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-02
 */
@Service
public class LivetelecastCategoryServiceImpl extends ServiceImpl<LivetelecastCategoryMapper, LivetelecastCategory> implements ILivetelecastCategoryService {

    @Autowired
    private LivetelecastCategoryMapper livetelecastCategoryMapper;

    @Override
    public ReturnVO<List<LivetelecastCategory>> listAllLivetelecastCategory(Long pid) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        queryWrapper.eq("pid", pid);
        List<LivetelecastCategory> livetelecastCategories = livetelecastCategoryMapper.selectList(queryWrapper);
        return new ReturnVO("展示所有直播分类成功", livetelecastCategories);
    }

    @Override
    public ReturnVO saveLivetelecastCategory(LivetelecastCategory livetelecastCategory) {
        if (livetelecastCategory.getId() == null) {
            livetelecastCategoryMapper.insert(livetelecastCategory);
        } else {
            livetelecastCategoryMapper.updateById(livetelecastCategory);
        }
        return new ReturnVO("新增直播分类成功", livetelecastCategory.getId());
    }

    @Transactional
    @Override
    public ReturnVO deleteLivetelecastCategory(Long id) {
        LivetelecastCategory livetelecastCategory = livetelecastCategoryMapper.selectById(id);
        if (livetelecastCategory != null && livetelecastCategory.getType() != 0) {
            throw new ServiceException("特殊分类无法删除");
        }
        livetelecastCategoryMapper.deleteById(id);
        return new ReturnVO("分类删除成功", id);
    }

    @Autowired
    private PlatformLivetelecastMapper platformLivetelecastMapper;

    @Override
    public ReturnVO<JgPageVo<List<LivetelecastCategory>>> listLivetelecastCategory(LivetelecastCategoryDto livetelecastCategoryDto) {
        PageHelper.startPage(livetelecastCategoryDto.getCurPage(), livetelecastCategoryDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", 0);
        if (StringUtils.isNotBlank(livetelecastCategoryDto.getCategoryName())) {
            queryWrapper.like("category_name", livetelecastCategoryDto.getCategoryName() + "%");
        }
        queryWrapper.orderByAsc("sort");
        List<LivetelecastCategory> livetelecastCategories = livetelecastCategoryMapper.selectList(queryWrapper);
        //查询旗下直播数量
        for (LivetelecastCategory livetelecastCategory : livetelecastCategories) {
            Integer count = 0;
            //查询一级分类下直播
            if (livetelecastCategory.getPid() == 0) {
                count = platformLivetelecastMapper.selectCount(new LambdaQueryWrapper<PlatformLivetelecast>()
                        .eq(PlatformLivetelecast::getLivetelecastCategoryIdFirst, livetelecastCategory.getId()));
            }
            //查询二级分类下直播
            if (livetelecastCategory.getPid() != 0) {
                count = platformLivetelecastMapper.selectCount(new LambdaQueryWrapper<PlatformLivetelecast>()
                        .eq(PlatformLivetelecast::getLivetelecastCategoryIdSecond, livetelecastCategory.getId()));
            }
            livetelecastCategory.setPlatformLivetelecastNum(count);
            List<LivetelecastCategory> secondCategories = livetelecastCategoryMapper.listLivetelecastCategoryByPid(livetelecastCategory.getId());
            livetelecastCategory.setChildren(secondCategories);
        }
        return new ReturnVO("直播分类查询成功", new JgPageVo<>(new PageInfo(livetelecastCategories)));
    }
}
